package com.example.customeview;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import org.jetbrains.annotations.NotNull;

/**
 * Created by Caleb.F
 * on 2020-02-16
 * at 14:18
 */
public class NotificationUtil {
    private Context mContext;
    private Integer mId;


    private NotificationManager mManager;

    public NotificationUtil(Context context, int Id) {
        mContext = context;
        mId = Id;
        mManager = getManager();
    }

    private NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public  PendingIntent createPendingIntent(Activity resultactivity) {
        Intent intent = new Intent(mContext, resultactivity.getClass());
        return PendingIntent.getActivity(mContext, mId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }


    public NotificationCompat.Builder getNotification(String title, String contentText) {
        return new NotificationCompat.Builder(mContext).setSmallIcon(android.R.drawable.stat_notify_chat)
                .setContentTitle(title)
                .setContentText(contentText)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setAutoCancel(true);
    }


    public void sendNotification(String notificationtitle, String notificationContent, @NotNull PendingIntent intent) {
        getManager().notify(mId,
                getNotification(notificationtitle, notificationContent).setContentIntent(intent).build());

    }
}
